package jp.mixi.android.sdk.hackathon.entity;

import jp.mixi.android.sdk.hackathon.util.JSONObjectUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class VoiceEntity {

    private List<Voice> list;

    public VoiceEntity(String src) {
        try {
            JSONObject root = new JSONObject(src);
            JSONArray array = root.getJSONArray("entry");
            list = new ArrayList<Voice>();
            for (int i = 0; i < array.length(); i++) {
                list.add(new Voice(array.getString(i)));
            }

        } catch (JSONException e) {
            throw new IllegalArgumentException("not json", e);
        }
    }

    public List<Voice> getVoices() {
        return list;
    }
    public class Voice {

        private JSONObject original;

        public Voice(String src) {
            try {
                original = new JSONObject(src);
            } catch (JSONException e) {
                throw new IllegalArgumentException("not json", e);
            }
        }

        public Voice(JSONObject src) {
            original = src;
        }

        public String getId() {
            return JSONObjectUtil.getStringByName(original, "id");
        }

        public String getCreatedAt() {
            return JSONObjectUtil.getStringByName(original, "created_at");
        }

        public final String getText() {
            return JSONObjectUtil.getStringByName(original, "text");
        }

        public final People getUser() {
            return createPeople(JSONObjectUtil.getObjectByName(original, "user"));
        }

        public final int getReplyCount() {
            return JSONObjectUtil.getIntByName(original, "reply_count");
        }

        public final int getFavoriteCount() {
            return JSONObjectUtil.getIntByName(original, "favorite_count");
        }

        public final boolean getFavorited() {
            return JSONObjectUtil.getBooleanByName(original, "favorited");
        }

        private final People createPeople(JSONObject src) {
            if (src == null) {
                return null;
            }
            try {
                JSONObject peopleObj = new JSONObject();
                peopleObj.put("id", JSONObjectUtil.getStringByName(src, "id"));
                peopleObj.put("displayName", JSONObjectUtil.getStringByName(src, "screen_name"));
                peopleObj.put("thumbnailUrl",
                        JSONObjectUtil.getStringByName(src, "profile_image_url"));
                peopleObj.put("profileUrl", JSONObjectUtil.getStringByName(src, "url"));

                return new People(peopleObj);
            } catch (JSONException e) {
                return null;
            }
        }

        public final String getImageUrl() {
            return JSONObjectUtil.getStringByName(getPhoto(original, 0), "image_url");
        }

        public final String getThumbnailUrl() {
            return JSONObjectUtil.getStringByName(getPhoto(original, 0), "thumbnail_url");
        }

        private JSONObject getPhoto(JSONObject src, int index) {
            JSONArray array = JSONObjectUtil.getJSONArrayByName(src, "photo");
            if (array == null || array.length() == 0) {
                return null;
            }
            return JSONObjectUtil.getObjectByIndex(array, index);
        }

        public final String getGender() {
            return JSONObjectUtil.getStringByName(original, "gender");
        }

        public final String getFieldByName(String name) {
            return JSONObjectUtil.getStringByName(original, name);
        }

        public String toString() {
            return getText();
        }
    }
}
